package com.book.controller;

import com.book.service.MenuService;
import com.book.service.RoleMenuService;
import com.book.util.JsonResult;
import com.book.vo.MenuVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * @author SuperYu  2024/6/24
 */
@RestController
@Slf4j
public class MenuController {

    @Resource
    MenuService menuService;

    @Resource
    RoleMenuService roleMenuService;


    @GetMapping("/api/getMenu")
    public JsonResult<List<MenuVo>> getMenu(String userName) {

        List<MenuVo> list = menuService.findMenuByUserName(userName);

        return JsonResult.ok(list);
    }

    // 得到所有的权限（父级带着子级）
    @GetMapping("/api/menu/getAllPermission")
    public JsonResult<List<MenuVo>> showAllPermission() {

        List<MenuVo> list = menuService.findAllPermission();

        return JsonResult.ok(list);
    }

    // 根据id得到当前角色的权限id
    @GetMapping("/api/menu/getAllPermissionByRoleId/{id}")
    public JsonResult<Set<Long>> getPermissionByRoleId(@PathVariable int id) {

        Set<Long> set = menuService.findPermissionByRoleId(id);

        return JsonResult.ok(set);
    }

    // 授权
    @PostMapping("/api/permission/grant")
    public JsonResult<String> doGrant(int roleId, int[] ids) {

        int i = roleMenuService.addRoleMenu(roleId, ids);
        if (i > 0) {
            return JsonResult.ok(i + "");
        }

        return JsonResult.ok(null);
    }

}


